/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lib;


import java.io.FileInputStream;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Thiago Campos Viana
 */
public class MapParser {

    public static byte[][] getMap(byte id, byte layerID, byte mapLayer) {
        byte[][] tileMap=null;
        InputStream is = null;
        try {
            is = new FileInputStream("data/maps/r" + id + "l" + layerID + ".tmx");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(is);
            doc.getDocumentElement().normalize();

            System.out.println("Root element " + doc.getDocumentElement().getNodeName());
            NodeList nodeLst = doc.getElementsByTagName("layer");
            //System.out.println(doc.getDocumentElement().getAttribute("width"));
            int width = (Integer.parseInt(doc.getDocumentElement().getAttribute("width")));
            int height = (Integer.parseInt(doc.getDocumentElement().getAttribute("height")));
            System.out.println("Information of all spawn points");
            tileMap = new byte[width][height];

            for (int s = 0; s < nodeLst.getLength(); s++) {

                Node fstNode = nodeLst.item(s);
                NamedNodeMap nn = fstNode.getAttributes();
                String name = (nn.getNamedItem("name").getTextContent());
                System.out.println(name);
                if (name.equals("layer"+mapLayer)) {
                    fstNode.normalize();
                    NodeList layer = fstNode.getChildNodes();
                    Node data = null;
                    for (int x = 0; x < layer.getLength(); x++) {
                        if (layer.item(x).getNodeName().equals("data")) {
                            data = layer.item(x);
                            break;
                        }
                    }
                    System.out.println(data.getNodeName());

                    int x = 0;
                    int y = 0;

                    for (int j = 0; j < data.getChildNodes().getLength(); j++) {
                        //System.out.println(data.getChildNodes().item(x).getNodeName());
                        if (data.getChildNodes().item(j).getNodeName().equals("tile")) {
                            tileMap[y][x] = (byte) (Integer.parseInt(data.getChildNodes().item(j).getAttributes().getNamedItem("gid").getTextContent())-101);
                            System.out.println("x=" + (x) + ";y=" + (y) + ";gid=" + tileMap[y][x]);

                            x++;
                            x = (x != width) ? x : 0;
                            y = (x != 0) ? y : y + 1;
                        }


                    }

                }
                /*
                if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
                Element fstElmnt = (Element) fstNode;
                NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("monster");
                System.out.println(fstNmElmntLst.item(0).getAttributes().getNamedItem("name").getTextContent());
                //System.out.println(nl.item(0).getAttributes().getNamedItem("name").getTextContent());
                /*
                Element fstElmnt = (Element) fstNode;
                NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("firstname");
                Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
                NodeList fstNm = fstNmElmnt.getChildNodes();
                System.out.println("First Name : "  + ((Node) fstNm.item(0)).getNodeValue());
                NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("lastname");
                Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
                NodeList lstNm = lstNmElmnt.getChildNodes();
                System.out.println("Last Name : " + ((Node) lstNm.item(0)).getNodeValue());
                 *
                 */
            }
        } catch (Exception ex) {

        }

        return tileMap;
    }
}
